package com.enjoy.studyfragment.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.enjoy.mjbaseframe.base.BaseSwipeBackFragment;
import com.enjoy.studyfragment.R;
import com.enjoy.studyfragment.widget.TopBarView;
import com.orhanobut.logger.Logger;

import butterknife.ButterKnife;

/**
 * fragment基础类，共享头部标题栏。
 * Created by YoKeyword on 16/2/7.
 */
public abstract class TopBarFragment extends BaseSwipeBackFragment{
    TopBarView.onTitleBarClickListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = null;
        if (setLayout() instanceof Integer) {
            rootView=inflater.inflate((Integer) setLayout(),container,false);
        }else if(setLayout() instanceof View){
            rootView= (View) setLayout();
        }else{
            throw new ClassCastException("type of setLayout() must be int or View!");
        }
        mUnbinder= ButterKnife.bind(this,rootView);
        init();
        rootView = addTopBar(rootView);
        return attachToSwipeBack(rootView);
    }

    /**
     * fragmen顶部添加头部标题栏TopBar
     * @param rootView
     * @return
     */
    public View addTopBar(View rootView){
        if(isTopBar()){
            LinearLayout linearLayout = new LinearLayout(getActivity());
            linearLayout.setOrientation(LinearLayout.VERTICAL);

            TopBarView topBarView = new TopBarView(getContext());
            setListener(listener);
            initTopBar(topBarView);

            linearLayout.addView(topBarView);
            linearLayout.addView(rootView);
            return linearLayout;
        }else {
            return rootView;
        }
    }

    /**
     * 是否添加头部标题栏TopBar
     * @return true 默认添加 false 不添加
     */
    public boolean isTopBar(){
        return true;
    }

    /**
     * 头部标题栏TopBar默认设置
     * @param topbar
     */
    public void initTopBar(TopBarView topbar){
        topbar.getTitleView().setTextSize(20);
        topbar.getTitleView().setTextColor(Color.WHITE);
        topbar.getTitleView().setText("标题");
        topbar.getBackView().setImageResource(R.mipmap.top_icon_return);
        topbar.setBackgroundResource(R.mipmap.top_bg);
        topbar.setClickListener(listener);
    }

    /**
     * 自定义子类头部导航点击事件
     * @param chlidListener
     */
    public void setListener(TopBarView.onTitleBarClickListener chlidListener){
        listener = chlidListener;
        if(listener == null){ //默认头部导航点击事件
            listener = new TopBarView.onTitleBarClickListener() {
                @Override
                protected void onBackClick() {
                    Logger.d("父类：onBackClick");
                    pop();
                }

                @Override
                protected void onRightClick() {

                }
            };
        }
    }
}
